<template>
  <articles-layout class="article-com">
    <template slot="main" v-if="article.publishTime">
      <h1>{{article.title || ''}}</h1>
      <div class="article__sub">
        <span>{{article.author || ''}}</span> <span>{{article.publishTime}}</span>
      </div>
      <div class="article__detail" v-html="article.content || ''">
      </div>
    </template>
    <side-container slot="side" title="相关资讯" :show-more="true" :more-href="moreHref">
      <side-list :data="recList"/>
    </side-container>
  </articles-layout>
</template>

<script>
  import ArticlesLayout from './components/ArticlesLayout'
  import SideContainer from './components/SideContainer'
  import SideList from './components/SideList'

  import {fieldState} from "@/mixins"

  import {fetchArticle} from '@/api/articleRequest'
  import {fetchList} from '@/api/homeRequest'

  export default {
    name: "article-com",
    mixins: [fieldState],
    props: ['id'],
    data() {
      return {
        article: {},
        recList: [],
        recCount: 10
      }
    },
    computed: {
      moreHref() {
        const vm = this;
        return vm.$router.resolve({name: 'listPage', params: {field: vm.field}}).href;
      }
    },
    watch: {
      id() {
        const vm = this;
        vm.fetchData();
      },
      article() {
        const vm = this;
        let navInfo = vm.$store.getters.getNavByKey(vm.article.navType);
        let navCode = '';
        if (navInfo !== null) {
          if (navInfo.code !== vm.field) {
            vm.commitField(navInfo.code);
          }
          navCode = navInfo.code;
        }
        vm.fetchRecList(navCode);
      }
    },
    methods: {
      fetchData() {
        const vm = this;
        fetchArticle(vm.id).then(res => {
          vm.article = res.data;
        });
      },
      fetchRecList(navCode) {
        const vm = this;
        fetchList(navCode, { pageSize: vm.recCount + 5 }).then(res => {
          vm.setRecList(res.data.items);
        });
      },
      setRecList(list = []) {
        const vm = this;
        list = vm.$util.randomSort(list);
        let result = [];
        for (let item of list) {
          if (item.item.dataId !== vm.id && result.length !== vm.recCount) {
            result.push(item.item);
          }
        }
        vm.recList = result;
      }
    },
    created() {
      const vm = this;
      vm.fetchData();
    },
    components: {
      SideContainer,
      SideList,
      ArticlesLayout
    }
  }
</script>

<style scoped>
  .articles__main h1{
    font-size: 26px;
    margin-bottom: 10px;
  }
  .article__sub {
    margin-bottom: 30px;
    margin-left: 5px;
  }
  .article__sub span{
    display: inline-block;
    color: #cbcbcb;
    font-size: 14px;
    margin-right: 10px;
  }
  .article__detail /deep/ *{
    text-indent: 2em;
    font-size: 16px;
    margin: 5px auto;
    line-height: 2em;
    text-align: justify;
  }
  .article__detail /deep/ img {
    min-width: 500px;
    width: 600px;
    max-width: 100%;
    display: block;
    margin: 10px auto;
  }

</style>
